package com.example.administrator.yuejian2.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.yuejian2.MainActivity;
import com.example.administrator.yuejian2.R;
import com.example.administrator.yuejian2.RetrofitListener.RetrofitListener;
import com.example.administrator.yuejian2.bean.RegisterBean;
import com.example.administrator.yuejian2.model.RegisterModel;
import com.example.administrator.yuejian2.model.UserModel;

public class RegisterActivity extends BaseActivity{


    private RetrofitListener<RegisterBean> listener;
    private Button register_button;
    private EditText user,password,tel,email;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        initEvents();
        listener=new RetrofitListener<RegisterBean>() {
            @Override
            public void onSuccess(RegisterBean registerBean, int flag) {
                if(registerBean.getSuccess().equals("1"))
                {
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                    finish();
                }
                else{
                    Toast.makeText(RegisterActivity.this, "用户名已被注册", Toast.LENGTH_SHORT).show();
                }

            }

            @Override
            public void onFail() {
                Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_SHORT).show();
            }
        };
    }
    private void initView(){
        register_button=findViewById(R.id.register_button);
        user=findViewById(R.id.username);
        password=findViewById(R.id.password);
        tel=findViewById(R.id.usertel);
        email=findViewById(R.id.useremail);
    }
    private void initEvents(){
        register_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                register();
            }
        });
    }
   private void register(){
       RegisterModel registerModel=new RegisterModel();
       registerModel.userRegister(user.getText().toString().trim(),password.getText().toString().trim(),email.getText().toString().trim(),tel.getText().toString().trim(),listener);
    }
}
